﻿using Acme.BookStore.Order;
using Essensoft.Paylink.Alipay;
using Essensoft.Paylink.Alipay.Domain;
using Essensoft.Paylink.Alipay.Request;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore.Controllers
{
    [ApiExplorerSettings(GroupName = "v2")]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AliPayController : ControllerBase
    {

    
        private readonly IAlipayClient _client;
        private readonly IOptions<AlipayOptions> _optionsAccessor;
        private readonly IOrderservices orderservices;
        private readonly IRepository<Reservations> reservationsRepository;

        public AliPayController(IAlipayClient client, IOptions<AlipayOptions> optionsAccessor, IOrderservices orderservices, IRepository<Reservations> reservationsRepository)
        {
            _client = client;
            _optionsAccessor = optionsAccessor;
            this.orderservices = orderservices;
            this.reservationsRepository = reservationsRepository;
        }


        /// <summary>
        /// 电脑网站支付
        /// </summary>
        /// <param name="viewModel"></param>
        [HttpPost]
        public async Task<IActionResult> PagePay(Guid id)
        {
            var order = await reservationsRepository.FirstOrDefaultAsync(x=>x.Id==id);

            var model = new AlipayTradePagePayModel
            {
                Body = "智慧酒店订单",//viewModel.Body,
                Subject =  "智慧酒店订单",//viewModel.Subject,
                TotalAmount = order.OutofPocketamount.ToString(),//viewModel.TotalAmount,
                OutTradeNo = order.Id.ToString(),
                ProductCode = "FAST_INSTANT_TRADE_PAY"
            };
            var req = new AlipayTradePagePayRequest();
            req.SetBizModel(model);
            req.SetNotifyUrl("");
            req.SetReturnUrl("");

            var response = await _client.PageExecuteAsync(req, _optionsAccessor.Value);
            return Content(response.Body, "text/html", Encoding.UTF8);
        }


        /// <summary>
        /// 手机网站支付
        /// </summary>
        /// <param name="id"></param>
        [HttpPost]
        public async Task<IActionResult> WapPay(Guid id)
        {
            var order = await reservationsRepository.FirstOrDefaultAsync(x => x.Id == id);
            if (order == null)
            {
                return NotFound("Order not found");
            }

            var model = new AlipayTradeWapPayModel
            {
                Body = "智慧酒店订单",
                Subject = "智慧酒店订单",
                TotalAmount = order.OutofPocketamount.ToString(),
                OutTradeNo = order.Id.ToString(),
                ProductCode = "QUICK_WAP_PAY"
            };

            var req = new AlipayTradeWapPayRequest();
            req.SetBizModel(model);
            req.SetNotifyUrl(""); // Set your notify URL here
            req.SetReturnUrl(""); // Set your return URL here

            var response = await _client.PageExecuteAsync(req, _optionsAccessor.Value);
            return Content(response.Body, "text/html", Encoding.UTF8);
        }
    }
}

